Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 5 - Association Rule Learning/Apriori/[Phython] Apriori.ipynb
1341 views
Kernel: Python 3

Apriori

Data preprocessing

# Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd %matplotlib inline plt.rcParams['figure.figsize'] = [14, 8]
# Importing the dataset dataset = pd.read_csv('Market_Basket_Optimisation.csv', header = None)
dataset.head(10)
transactions = [] for i in range(0, 7501): transactions.append([str(dataset.values[i, j]) for j in range(0, 20)])
transactions[0]
['shrimp', 'almonds', 'avocado', 'vegetables mix', 'green grapes', 'whole weat flour', 'yams', 'cottage cheese', 'energy drink', 'tomato juice', 'low fat yogurt', 'green tea', 'honey', 'salad', 'mineral water', 'salmon', 'antioxydant juice', 'frozen smoothie', 'spinach', 'olive oil']
type(transactions)
list
len(transactions)
7501

Training Apriori on the dataset

from apyori import apriori rules = apriori(transactions, min_support = 0.003, min_confidence = 0.02, min_lift = 3, min_length = 2)

Visualization of the result

result = list(rules)
result[0]
RelationRecord(items=frozenset({'cottage cheese', 'brownies'}), support=0.0034662045060658577, ordered_statistics=[OrderedStatistic(items_base=frozenset({'brownies'}), items_add=frozenset({'cottage cheese'}), confidence=0.10276679841897232, lift=3.225329518580382), OrderedStatistic(items_base=frozenset({'cottage cheese'}), items_add=frozenset({'brownies'}), confidence=0.10878661087866107, lift=3.2253295185803816)])
dir(result[0])
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_asdict', '_fields', '_make', '_replace', '_source', 'count', 'index', 'items', 'ordered_statistics', 'support']
dir(result[0].ordered_statistics[0])
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_asdict', '_fields', '_make', '_replace', '_source', 'confidence', 'count', 'index', 'items_add', 'items_base', 'lift']
for i in range(0, 11): print('association:', result[i].items) print('support:',result[i].support) print('confidence:', result[i].ordered_statistics[0].confidence) print('lift:', result[i].ordered_statistics[0].lift, '\n')
association: frozenset({'cottage cheese', 'brownies'}) support: 0.0034662045060658577 confidence: 0.10276679841897232 lift: 3.225329518580382 association: frozenset({'light cream', 'chicken'}) support: 0.004532728969470737 confidence: 0.07555555555555556 lift: 4.843950617283951 association: frozenset({'escalope', 'mushroom cream sauce'}) support: 0.005732568990801226 confidence: 0.0722689075630252 lift: 3.7908326967150496 association: frozenset({'escalope', 'pasta'}) support: 0.005865884548726837 confidence: 0.07394957983193277 lift: 4.700811850163794 association: frozenset({'fresh bread', 'tomato juice'}) support: 0.004266097853619517 confidence: 0.09907120743034055 lift: 3.2593558198902826 association: frozenset({'fresh tuna', 'honey'}) support: 0.003999466737768298 confidence: 0.17964071856287428 lift: 3.7850703088205613 association: frozenset({'fromage blanc', 'honey'}) support: 0.003332888948140248 confidence: 0.2450980392156863 lift: 5.164270764485569 association: frozenset({'ground beef', 'herb & pepper'}) support: 0.015997866951073192 confidence: 0.1628222523744912 lift: 3.291993841134928 association: frozenset({'ground beef', 'tomato sauce'}) support: 0.005332622317024397 confidence: 0.054274084124830396 lift: 3.840659481324083 association: frozenset({'olive oil', 'light cream'}) support: 0.003199573390214638 confidence: 0.20512820512820515 lift: 3.1147098515519573 association: frozenset({'olive oil', 'whole wheat pasta'}) support: 0.007998933475536596 confidence: 0.12145748987854252 lift: 4.1224100976422955